#!/usr/bin/perl
use strict;
package nsnewpass;
sub nsnewpass {

    use Digest::MD5 qw(md5_base64);
    my ($nsnum,$dbh,$remote,$input4,$input5,$input0) = @_;
    my $sth;
#isauth or not
    $sth=$dbh->prepare("SELECT num,auth FROM ninfo WHERE num = '$input0';");
    $sth->execute;
    my ($isauth);
    while (my @tmp = $sth->fetchrow_array) {
	if ($tmp[0] eq $input0) {
	    $isauth = $tmp[1];
	}
	undef @tmp;
    }
#Get old passwd..
    my $sth=$dbh->prepare("SELECT password FROM nicks WHERE rnick = '$isauth';");
    $sth->execute;
    my $oldpass = $sth->fetchrow_array;
    if ($input5 eq "") {
	print $remote "$nsnum O $input0 :Syntax: NEWPASS <oldpass> <newpass>\n";
    }
    elsif ($isauth eq '0') {
	print $remote "$nsnum O $input0 :You have not identified yourself with Nickserv yet.\n";
    }
    elsif ($oldpass ne md5_base64($input4)) { 
	print $remote "$nsnum O $input0 :Incorrect password.\n";
    }
    elsif (length($input5) < 5 || length($input5) > 20) {
	print $remote "$nsnum O $input0 :Password must be at least 5 chars (20 is max).\n";
    }
    else {
	my $passwd = md5_base64($input5);
	$sth=$dbh->prepare("UPDATE nicks SET password = '$passwd' WHERE rnick = '$isauth';");
	$sth->execute; 
	print $remote "$nsnum O $input0 :Changed password to $input5\n";
    }
}
1;
